package Interface.Insumos.Unidade;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import sft.BancodeDados.Dao;
import sft.Logica.Insumos;

public class UnidadeDao extends Dao{
    
    public UnidadeDao() throws SQLException {
    }
    
    public void Adicionar(Insumos p) throws SQLException{
        p.setSql("insert into sft_unid (unid_nm_nmunid, unid_cod_codunid) values (?, ?)");
        Operacao("adicionar", p);
    }
    public void Editar(Insumos p) throws SQLException{
        p.setSql("update sft_unid set unid_nm_nmunid=?, unid_cod_codunid=? where unid_id_idunid like ?");
        Operacao("editar", p);
    }
    public void Deletar(Insumos p) throws SQLException{
        p.setSql("DELETE FROM sft_unid WHERE unid_id_idunid like ?");
        Operacao("deletar", p);
    }

    @Override
    public void Operacao(String caso, Insumos l) throws SQLException{
        try (PreparedStatement stmt = conexao.prepareStatement(l.getSql())) {
            switch(caso){
                case "adicionar":
                    setAdd(stmt, l);
                    break;
                    
                case "editar":
                    setUpdate(stmt, l);
                    break;
                    
                case "deletar":
                    setDelete(stmt, l);
                    break;                
            }                
            stmt.execute();
            stmt.close();
            conexao.close();
        }
        
    }
    
    private void setAdd(PreparedStatement stmt, Insumos l) throws SQLException {
        try {
            stmt.setString(1, l.getNome());
            stmt.setInt(2, l.getCod());
        } catch (Exception e) {
            stmt.setString(1, l.getNome());
        }
    }

    private void setUpdate(PreparedStatement stmt, Insumos l) throws SQLException {
        try {
            stmt.setString(1, l.getNome());
            stmt.setInt(2, l.getCod());
            stmt.setInt(3, l.getId());
            
        } catch (Exception e) {
            stmt.setString(1, l.getNome());
            stmt.setInt(2, l.getId());
        }
    }

    private void setDelete(PreparedStatement stmt, Insumos l) throws SQLException {
        stmt.setInt(1, l.getId());
    }
}
